![]() | Application Integration (JavaScript Engine) | Deploying the Knowledge (Web Browser) | ![]() |
The following code can be used to replace 'main.html' from the root folder of a deployment.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script language="javascript" src="jquery-1.8.2.min.js"></script>
<script language="javascript" src="xr_engine-min.js"></script>
<script language="javascript" src="objects/dict.js"></script>
<script>
function OnStart(xpertrule) {
/* Called at the START of inderence, use this to setup initial values */
xpertrule.val("Grade", "Senior_Manager"); // e.g. Set the Grade object to Senior_Manager
xpertrule.val("Department", "Accounts");
xpertrule.val("Cost", 75);
xpertrule.val("In_London", true);
xpertrule.val("Services", ["Room","Meals"]);
}
function OnPause(xpertrule, aItem) {
/* Called in response to an @PAUSE knowledgeBuilder command, or, an empty attribute, or, a dialog */
alert("Pause : " + aItem);
return true; // synchronous call, continue inference on return
// return false; // asynchronous call. You must call xpertrule.continue to continue inference
}
function OnDebug(xpertrule, aText) {
/* Called in response to an @DEBUG knowledgeBuilder command, or, an xpertrule.message call */
alert("Debug : " + aText);
}
function OnEnd(xpertrule, complete) {
/* Called at END of inference, use this to retrieve any decisions from the knowledge base */
var aVal = xpertrule.val("Expenses"); // Get the contents of the Expenses object
var aVal2 = xpertrule.val("Claims"); // Get the contents of the Claims object
document.getElementById("res").innerHTML = "Result : " + aVal + " / " + aVal2;
}
function OnError(xpertrule, aDescription) {
/* Called in response to an engine error */
alert("Error : " + aDescription);
}
$(function(){
/* This is how inference is started, in this example, called from page load */
xpertrule.startup({
xrengine: true, /* Required */
startCallback: OnStart, /* Optional (see comments in OnStart function above) */
pauseCallback: OnPause, /* Optional */
debugCallback: OnDebug, /* Optional */
endCallback: OnEnd, /* Optional */
errorCallback: OnError /* Optional */
});
});
</script>
</head>
<body>
This is a sample page to demonstrate using the silent use of the XpertRule Knowledge Builder Engine<br>
<span id="res"></span>
</body>
</html>
The following code can be used in a wrapper page which runs the deployed application in an iframe ie. <iframe src="deploy/main.html"></iframe>
<script>
// called from xpertrule.pause command. Function name matches that in the Knowledge Base Initialize procedure (shown below)
function OnPause(xpertrule, param) {
switch(param){
case "INIT":
xpertrule.val("Case_Id", 12345);
break;
}
}
// called at end of inference. Function name matches that in the Knowledge Base Initialize procedure (shown below)
function OnEnd(xpertrule) {
alert(xpertrule.val("Case_Result"));
}
</script>
The Knowledge Base Initialize procedure (mentioned in the script above) defines and uses the callbacks.
// Setup the pause callback to call the PauseCallback() function on the wrapper page
wrapperOptions.pauseCallback = parent.OnPause;
wrapperOptions.endCallback = parent.OnEnd;
// do initial pause to tell wrapper that it can set values
xpertrule.pause("INIT");